<?php 


class Category{
	
	public static $msgs = array();
	var $userid = '';
		
	function __construct()
	{
		$this->user_id = $_SESSION['admin_id'];
	}
	
	/* 
	***  Public Functions
	*/
	
	 public function addCategory($data)
	 {
	    
	 	global $db, $msgError;		
		
		trim_array($data);
		
		if($data['category_name'] == '')
		{
			$this->msgs[] = 'Category name can not be empty.';
		}
		if( $this->isCategoryExist($data['category_name'])){
			$this->msgs[] = 'Category name already in use.';
		}
		
	
		if( empty($this->msgs) )
		{
			 $record = array(
			             	'category_name' => $data['category_name']							
					       );
		 
			 if( $db->insert("category", $record) ) {
				redirect("admin/category.php?added=1");		
			 }
		}
		
		$msgError = '';
		foreach($this->msgs as $msg)
		{
			$msgError .= '<p>'.$msg.'</p>';
		}

		
	 }
	
		
	 public function updateCategory($data)
	 {
 		global $db, $msgError;	
		$category_id = ($data['category_id']);
		
		if(trim($data['category_name']) == '')
		{
			$this->msgs[] = 'Category name can not be empty.';
		}
		
		if( $this->isCategoryExist($data['category_name'] ,$category_id)){
			$this->msgs[] = 'Category name already in use.';
		}
	
		if( empty($this->msgs) )
		{
			 $record = array('category_name' => $data['category_name']);
			 $where = array( "category_id" =>$category_id );
			 if( $db->update("category", $record, $where) )
			 {
				redirect("admin/category.php?updated=1");		
			 }
		}
		
		$msgError = '';
		foreach($this->msgs as $msg)
		{
			$msgError .= '<p>'.$msg.'</p>';
		}
	 			
	}
	
	function getCategories($data)
	{
		
		global $db, $pager, $config;
		$pager = new Paginator();
		$keyword = isset($data['keyword']) ? trim($data['keyword']) : '';
		
		if( $keyword != '')
		{
			$where = " WHERE category_name like '%" . $db->escape($keyword) . "%'";
		}

		$config['page_limit'] =	( intval($data['new_page_limit']) > 0 ) ? $data['new_page_limit'] : $config['page_limit'];
		$page_limit = $config['page_limit']; 
		if(isset($_GET['pg'])&& $_GET['pg'] > 0 ){ 
			$limit_lenght  = (($_GET['pg']-1)*$page_limit) ; 
			$limit = "limit $limit_lenght, $page_limit ";
		}else{
			$limit = "limit 0 , $page_limit ";
			if($data['new_page_limit'] == 'all') 
				$limit = '';
		}

		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM category $where order by category_name $limit ";
		$result = $db->fetch_all($sql);
 		$counter = $db->queryCount();
		$pager->items_total = $counter[0];
		$pager->default_ipp = ($data['new_page_limit'] == 'all') ? $pager->items_total : $config['page_limit'];
		$pager->paginate($path);
		return $result;
	
	}
	
	
	
	
	 function getCategoryById($category_id)
	 {
	 	global $db;	
		$data = $db->first("SELECT * FROM category WHERE category_id = ".intval($category_id));
		return $data;
	 }
	
	
	function deleteCategory($category_id)
	{
		global $db, $msgError;
		
		$query = $db->query("SELECT * FROM quiz_question WHERE category_id = ".intval($category_id));
		if( $db->num_rows($query) > 0 )
		{
			$this->msgs[] = 'This category is associated questions, Can not delete this category.';	
		}
		
		if( empty($this->msgs) )
		{
			$where = array( 'category_id' => $category_id );
			if( $db->delete("category", $where) )
			{ 
				redirect("admin/category.php?deleted=1");	
			}
		}
		$msgError = '';
		foreach($this->msgs as $msg)
		{
			$msgError .= '<p>'.$msg.'</p>';
		}
		
	}
	
	public function isCategoryExist($category_name, $category_id = '')
	{
		global $db;
		$where = '';
		if($dismissal_type_id != '')
		{
			$where = ' AND category_id != '.($category_id);
		}
	
		
		$query = $db->query("select * from category where category_name = '".$db->escape($category_name)."' $where "); 
		
		if( $db->num_rows($query) > 0 )
		{	
			return TRUE;
		}	
		else
		{
			return FALSE;	
		}
	}
}


?>